package com.zlzlib.libroot;

import android.os.Build;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @DateTime: 2024/10/12 10:06
 * @Author zlz
 * @Version 1.0
 */
public final class RootUtil {
    /**
     * 开启adb服务 网络远程操作服务
     */
    public static boolean startAdb() {
        //开启adb 服务
        String command1 = "setprop service.adb.tcp.port 5555";
        //设置adb默认开启
        String command2 = "on property:persist.service.adb.enable=1";
        //重启adb服务 生效设置
        String command3 = "stop adbd";
        String command4 = "start adbd";
        //adb监听TCP端口5555，并允许通过该端口来进行无线的ADB连接
        String command5 = "adb tcpip 5555";
        return runCommand(command1, command2, command3, command4, command5);
    }

    /**
     * 设置时间
     *
     * @param time 时间戳
     * @return
     */
    public static boolean setDateTime(long time) {
        String command;
        if (Build.VERSION.SDK_INT >= 23) {
            String date =getMyDateFormat(time, "MMddHHmmyyyy.ss");
            command = "date " + date + "\n";
        } else {
            String date = getMyDateFormat(time, "yyyyMMdd.HHmmss");
            command = "date -s " + date + "\n";
        }
        return runCommand(command);
    }

    public static String getMyDateFormat(long time, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format, Locale.getDefault());
        df.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
        Date date = new Date(time);
        return df.format(date);
    }

    /**
     * 设置中国时区
     *
     * @return
     */
    public static boolean setTimeZoneChina() {
        return setTimeZone("Asia/Shanghai");
    }

    /**
     * 设置时区
     *
     * @param timeZone 时区代码
     * @return
     */
    public static boolean setTimeZone(String timeZone) {
        String command = "setprop persist.sys.timezone " + timeZone;
        return runCommand(command);
    }


    /**
     * @return 截图的进程
     * @throws IOException
     */
    public static Process getScreenCap() throws IOException {
        Process sh = getProcess();
        OutputStream os = sh.getOutputStream();
        os.write(("screencap -p \n").getBytes());
        os.flush();
        os.close();
        return sh;
    }

    /**
     * 安装程序
     *
     * @param apkPath 程序文件地址
     * @return
     */
    public static boolean installApk(String apkPath) {
        String command = "pm install -r " + apkPath;
        return runCommand(command);
    }

    /**
     * 卸载程序
     *
     * @param packName 程序包名
     * @return
     */
    public static boolean uninstallApk(String packName) {
        String command = "pm uninstall " + packName;
        return runCommand(command);
    }

    private static boolean runCommand(String... command) {
        Process process = getProcess();
        if (process == null) {
            return false;
        }
        try {
            PrintWriter printWriter = new PrintWriter(process.getOutputStream());
            for (String s : command) {
                printWriter.println(s);
            }
            // 提交命令并关闭写入流
            printWriter.flush();
            printWriter.close();
            // 等待命令执行完成
            return process.waitFor() == 0;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            process.destroy();
        }
        return false;
    }

    /**
     * @return root进程 有可能为空
     */
    public static Process getProcess() {
        Process process;
        try {
            process = Runtime.getRuntime().exec("su");
        } catch (IOException e) {
            process = null;
        }
        return process;
    }
}
